package bt



func OnMethedProcess(res *Response,req *Request) (error) {

	if StopString == req.Event {
		OnStopMethod(res,req)
	}else if ComplateString == req.Event{
		OnComplateMethod(res,req)
	} else if StartString == req.Event {
		if req.Left == 0 {
			req.Event = ComplateString
			OnComplateMethod(res,req)
		} else {
			OnStartMethod(res,req)
		}

	}else if ActiveString == req.Event  {

		if req.Left == 0 {
			req.Event = ComplateString
			OnComplateMethod(res,req)
		} else {
			req.Event = StartString
			OnStartMethod(res,req)
		}
	}

	return nil
}

func OnStartMethod(res *Response,req *Request) {

	btorrent ,err := Engine().Session.GetTorrent(req.InfoHash)
	if err != nil {
		return
	}

	btorrent.StartPeer(req)
	res.Peers += string(btorrent.GetPeer(req))

}
func OnStopMethod(res *Response,req *Request) {
	btorrent ,err := Engine().Session.GetTorrent(req.InfoHash)
	if err != nil {
		return
	}

	btorrent.StopPeer(req)
}
func OnComplateMethod(res *Response,req *Request) {

	btorrent ,err := Engine().Session.GetTorrent(req.InfoHash)
	if err != nil {
		return
	}
	btorrent.ComplatePeer(req)
	res.Peers += string(btorrent.GetPeer(req))

}

